<template>
  <div>
    <div class="top"><strong>首页</strong> > 权限管理 > 角色列表</div>
    <el-card>
      <el-button type="primary" @click="dialogVisible = true"
        >添加用户</el-button
      >
      <el-table :data="rolesList" border stripe  class="poos">
        <el-table-column type="expand">
          <template slot-scope="hop">
            <el-row
              v-for="(m, i1) in hop.row.children"
              :key="m.id"
              :class="['pof', i1 === 0 ? 'tof' : '']"
            >
              <!-- 一级权限 -->
              <el-col :span="3">
                <el-tag
                  class="eltag"
                  closable
                  @close="removeby(hop.row, m.id)"
                  >{{ m.authName }}</el-tag
                >
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级权限 -->
              <el-col :span="19">
                <el-row
                  v-for="(m1, i2) in m.children"
                  :key="m1.id"
                  :class="[i2 === 0 ? '' : 'tof']"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      class="eltag"
                      closable
                      @close="removeby(hop.row, m1.id)"
                      >{{ m1.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 三级权限 -->
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      v-for="m2 in m1.children"
                      :key="m2.id"
                      class="eltag"
                      closable
                      @close="removeby(hop.row, m2.id)"
                      >{{ m2.authName }}</el-tag
                    >
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"> </el-table-column>
        <el-table-column label="角色名称" prop="roleName" width="200px">
        </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc" width="200px">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="vog">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="log(vog.row.id)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="remove(vog.row.id)"
              >删除</el-button
            >
            <el-button
              type="warning"
              icon="el-icon-setting"
              @click="yoss(vog.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      title="添加用户"
      :visible.sync="dialogVisible"
      width="50%"
      @close="addlogclose"
    >
      <el-form
        :model="addForm"
        :rules="addform"
        ref="addFromRef"
        label-width="90px"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="addForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="logb">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑区 -->
    <el-dialog
      title="编辑用户"
      :visible.sync="pott"
      width="50%"
      @close="addlogclose"
    >
      <el-form
        :model="addForm"
        :rules="addform"
        ref="addFromRef"
        label-width="90px"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="addForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="pott = false">取 消</el-button>
        <el-button type="primary" @click="pog">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="分配权限" :visible.sync="bott" width="50%"  @close="alogclose">
      <!-- 树形组件 -->
      <el-tree
        :data="alllist"
        :props="defaultProps"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="keyy"
        ref='treeref'
      ></el-tree>

      <span slot="footer" class="dialog-footer">
        <el-button @click="bott = false">取 消</el-button>
        <el-button type="primary" @click="hotkey">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.roleslist()
  },
  data() {
    return {
     
      roleid:'',
      keyy: [],
      rolesList: [],
      alllist: [],
      //树形控件属性对象的绑定对象
      defaultProps: {
        label: 'authName',
        children: 'children',
      },
      bott: false,
      addform: {
        roleName: [
          {
            required: true,
            messgae: '请输入角色名',
            trigger: 'blur',
          },
          {
            min: 0,
            max: 5,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        roleDesc: [
          {
            required: true,
            messgae: '请输入角色描述',
            trigger: 'blur',
          },
          {
            min: 1,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
      },
      addForm: {
        roleName: '',
        roleDesc: '',
      },
      dialogVisible: false,
      pott: false,
    }
  },
  methods: {
    async roleslist() {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) {
        return this.$message.error('res.meta.msg')
      }
      this.rolesList = res.data
      //console.log(this.rolesList)
    },
    addlogclose() {
      this.$refs.addFromRef.resetFields()
    },
    alogclose(){
this.keyy=[]
    },
    logb() {
      this.$refs.addFromRef.validate(async (valid) => {
        console.log(valid)
        if (!valid) return
        const { data: res } = await this.$http.post('roles', this.addForm)
        if (res.meta.status != 201) {
          this.$message.error('添加用户失败')
        } else {
          this.$message.success(res.meta.msg)
          this.dialogVisible = false
          //重新获取数据列表
          this.roleslist()
        }
      })
    },
    async log(ht) {
      console.log(ht);
      const { data: res } = await this.$http.get('roles/' + ht)
      console.log(res);
      if (res.meta.status !== 200) {
        this.$message.error('111')
      }
      this.addForm = res.data
      this.pott = true
    },
    pog() {
      this.$refs.addFromRef.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$http.put(
          'roles/' + this.addForm.roleId,
          this.addForm
        )
        if (res.meta.status !== 200) {
          this.$message.error(res.meta.msg)
        } else {
          this.$message.success(res.meta.msg)
          this.pott = false
          //重新获取数据列表
          this.roleslist()
        }
      })
    },
    async remove(id) {
      const bug = await this.$confirm(
        '此操作将永久删除该用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      console.log(bug)
      //成功时是confirm
      //取消是cancel
      if (bug !== 'confirm') {
        this.$message.info('已取消删除')
      } else {
        const { data: res } = await this.$http.delete(`roles/${id}`)
        if (res.meta.status !== 200) {
          return this.$message.error('删除用户失败')
        }
        this.$message.success('删除成功')
        this.roleslist()
      }
    },
    async removeby(s, k) {
      const bug = await this.$confirm(
        '此操作将永久删除该权限, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      console.log(bug)
      //成功时是confirm
      //取消是cancel
      if (bug !== 'confirm') {
        this.$message.info('已取消删除')
      } else {
        const { data: res } = await this.$http.delete(
          `roles/${s.id}/rights/${k}`
        )
        console.log(res)
        if (res.meta.status !== 200) {
          return this.$message.error('删除权限失败')
        }
        this.$message.success('删除成功')
        //console.log(res)
        s.children = res.data
      }
    },

    async yoss(role) {
      this.roleid=role.id
      console.log(role)
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) {
        return this.$messgae.error(res.meta.msg)
      }
      this.alllist = res.data
      this.getboss(role, this.keyy)
      this.bott = true
    },
    getboss(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach((item) => this.getboss(item, arr))
    },
    async hotkey(){
         const keys = [
           ...this.$refs.treeref.getCheckedKeys(),
           ...this.$refs.treeref.getHalfCheckedKeys()
         ]
         const idstr = keys.join(',')
         const{data:res}=await this.$http.post(`roles/${this.roleid}/rights`,{rids:idstr})
         if(res.meta.status!==200){
           return this.$message.error(res.meta.msg)
         }
         console.log(res);
         this.$message.success(res.meta.msg)
          this.roleslist()
         this.bott=false
    }
  },
}
</script>

<style lang='less' scoped>
.top {
  margin-left: 20px;
  margin-top: 25px;
  font-size: 14px;
  margin-bottom: 20px;
}
.eltag {
  margin: 7px;
}
.tof {
  border-top: 1px solid #eee;
}
.pof {
  border-bottom: 1px solid #eee;
}
.poos{
  margin-top: 10px;
}
</style>